package com.rankeiot.core.anno.aspect;

import com.rankeiot.core.Current;
import com.rankeiot.core.anno.DataScope;
import com.rankeiot.core.data.IScoped;
import com.rankeiot.core.data.UserInfo;
import com.rankeiot.core.sqltoy.ScopeExecutor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class DataScopeAspect {

    @Before("@annotation(scope)")
    public void before(JoinPoint point, DataScope scope){
       for(Object obj:point.getArgs()){
           if(obj==null){
               continue;
           }
           if(obj instanceof IScoped){
               UserInfo u=Current.user(false);
               String sc=" and 1==0 ";
               if(u!=null){
                   sc=u.getScopeSql(scope.user(), scope.branch(), scope.role());
               }
               ((IScoped)obj).setScope(sc);
           }
       }
   }
}
